package org.hive.testserver.domain.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestRunDetails extends TestRunSummary {

	private List<TestSummary> tests = new ArrayList<TestSummary>();
	private List<WorkSummary> slaves = new ArrayList<WorkSummary>();
	private List<WorkSummary> works = new ArrayList<WorkSummary>();
	
	public TestRunDetails(TestRun testRun, WorkSheet workSheet) {
		super(testRun);
		initializeTests(testRun, workSheet);
		initializeSlaves(workSheet);
		initializeWorks(workSheet);
	}

	private void initializeSlaves(WorkSheet workSheet) {
		for (Slave slave : workSheet.slaves()) {
			slaves.add(new WorkSummary(slave, workSheet.currentWorkForSlaveOrNull(slave)));
		}
	}

	private void initializeWorks(WorkSheet workSheet) {
		for (Work work : workSheet.works()) {
			works.add(new WorkSummary(work));
		}
	}

	private void initializeTests(TestRun testRun, WorkSheet workSheet) {
		for (Test test : testRun.getTests()) {
			TestSummary testSummary = new TestSummary(test, workSheet.findWorksForTest(test));
			tests.add(testSummary);
		}
	}
	
	public List<TestSummary> testsSummary() {
		return Collections.unmodifiableList(tests);
	}

	public List<WorkSummary> getSlaves() {
		return Collections.unmodifiableList(slaves);
	}
	
	public List<WorkSummary> getWorks() {
		return Collections.unmodifiableList(works);
	}
	
	public TestSummary lookupTestByNameOrNull(String testName) {
		for (TestSummary test : tests) {
			if (test.getName().equals(testName)) {
				return test;
			}
		}
		return null;
	}
}
